package bit;

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @author ZJX
 * @version 1.0
 * @descption
 * @since 2025/11/18 19:25
 */
public class Day27 {
    /**
     * 计算成功举办活动需要多少名主持人
     *
     * @param n        int整型 有n个活动
     * @param startEnd int整型二维数组 startEnd[i][0]用于表示第i个活动的开始时间，startEnd[i][1]表示第i个活动的结束时间
     * @return int整型
     */
    public int minmumNumberOfHost(int n, int[][] startEnd) {
        int ret = 0;
        int[] start = new int[n];
        int[] end = new int[n];
        for (int i = 0; i < n; i++) {
            start[i] = startEnd[i][0];
            end[i] = startEnd[i][1];
        }
        Arrays.sort(start);
        Arrays.sort(end);
        int endPtr = 0;
        for (int i = 0; i < n; i++) {
            if (start[i] < end[endPtr]) {
                ret++;
            } else {
                endPtr++;
            }
        }
        return ret;
    }

    public int minmumNumberOfHostPQ(int n, int[][] startEnd) {
        Arrays.sort(startEnd, (a, b) -> Integer.compare(a[0], b[0]));
        PriorityQueue<Integer> heap = new PriorityQueue<>(); // 存放结束时间
        heap.offer(startEnd[0][1]);
        for (int i = 1; i < n; i++) {
            if (startEnd[i][0] >= heap.peek()) {
                heap.poll(); // 主持人复用
            }
            heap.offer(startEnd[i][1]);
        }
        return heap.size();
    }

    private static int MOD = 109;

//    public static void main(String[] args) {
//        Scanner in = new Scanner(System.in);
//        int n = in.nextInt();
//        int m = in.nextInt();
//        int ret = n;
//        for (int i = 0; i < m - 1; i++) {
//            ret = ret * (n - 1) % MOD;
//        }
//        System.out.println(ret);
//    }

    public static void main(String[] args) throws IOException {
        // 初始化StreamTokenizer（从标准输入读取）
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);

        // 读取n（行数）和m（列数）
        in.nextToken(); // 推进到第一个数字
        int n = (int) in.nval; // 获取n
        in.nextToken();
        int m = (int) in.nval; // 获取m

        // 读取起点(xs, ys)和终点(xt, yt)，并转换为0-based索引
        in.nextToken();
        int xs = (int) in.nval - 1;
        in.nextToken();
        int ys = (int) in.nval - 1;
        in.nextToken();
        int xt = (int) in.nval - 1;
        in.nextToken();
        int yt = (int) in.nval - 1;

        // 读取网格（每行是一个字符串，用BufferedReader更方便）
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char[][] grid = new char[n][m];
        for (int i = 0; i < n; i++) {
            grid[i] = br.readLine().toCharArray(); // 直接读取整行
        }

        if (grid[xt][yt] == '*') {
            out.println(-1); // 提前返回
            out.close();
            return;
        }

//        起点(xs, ys)和终点(xt, yt)
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{xs, ys, 0});
        boolean[][] visit = new boolean[n][m];
        visit[xs][ys] = true;
        int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 方向数组
        while (!queue.isEmpty()) {
            int[] poll = queue.poll();
            int x = poll[0], y = poll[1], step = poll[2];
            if (x == xt && y == yt) {
                out.println(step);
                out.close();
                return;
            }

            for (int[] dir : dirs) {
                int nx = x + dir[0];
                int ny = y + dir[1];
                if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] == '.' && !visit[nx][ny]) {
                    visit[nx][ny] = true;
                    queue.offer(new int[]{nx, ny, step + 1});
                }
            }
        }
        out.println(-1);
        out.close();
    }

    public int threeSumClosest(int[] nums, int target) {
        int n = nums.length;
        // 边界处理：数组长度等于3，直接返回三数之和
        if (n == 3) {
            return nums[0] + nums[1] + nums[2];
        }
        Arrays.sort(nums);
        int ret = nums[0] + nums[1] + nums[2];
        int diff = Math.abs(ret - target);
        for (int i = 0; i < n - 2; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int l = i + 1;
            int r = n - 1;
            while (r > l) {
                int curSum = nums[i] + nums[l] + nums[r];
                int curDiff = Math.abs(curSum - target);
                if (diff > curDiff) {
                    diff = curDiff;
                    ret = curSum;
                }
                if (curDiff == 0) {
                    return curSum;
                }
                if (curSum < target) {
                    l++;
                } else {
                    r--;
                }
            }
        }
        return ret;
    }
}

